package com.huaxin.device.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huaxin.device.dto.ResponseResult;
import com.huaxin.device.dto.request.ElectricalRoomCreateReqDTO;
import com.huaxin.device.dto.request.ElectricalRoomPageListReqDTO;
import com.huaxin.device.dto.request.ElectricalRoomUpdateReqDTO;
import com.huaxin.device.dto.response.ElectricalRoomWithStatisticDTO;
import com.huaxin.device.models.ElectricalRoom;
import com.huaxin.device.service.ElectricalRoomService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "配电房管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/device/electricalRoom")
public class ElectricalRoomController {

    private final ElectricalRoomService service;

    @Operation(summary = "创建配电房", description = "创建新的配电房信息")
    @PostMapping("/create")
    public ResponseResult<ElectricalRoom> create(@RequestBody @Valid ElectricalRoomCreateReqDTO in) {
        return service.create(in);
    }

    @Operation(summary = "获取配电房", description = "根据ID获取配电房详情")
    @GetMapping("/get")
    public ResponseResult<ElectricalRoom> get(
            @Parameter(description = "配电房ID", example = "876e5432-e12d-32c1-b654-321098765432")
            @RequestParam("id") String id) {
        return ResponseResult.ok(service.getById(id));
    }

    @Operation(summary = "更新配电房", description = "更新配电房信息")
    @PostMapping("/update")
    public ResponseResult<String> update(@RequestBody @Valid ElectricalRoomUpdateReqDTO in) {
        return service.update(in);
    }

    @Operation(summary = "删除配电房", description = "删除配电房（启用状态不可删）")
    @DeleteMapping("/{id}/delete")
    public ResponseResult<String> delete(@PathVariable String id) {
        return service.delete(id);
    }

    @Operation(summary = "分页查询配电房", description = "支持按名称、编号、楼层等条件分页查询")
    @PostMapping("/page")
    public ResponseResult<IPage<ElectricalRoom>> pageQuery(@RequestBody @Valid ElectricalRoomPageListReqDTO in) {
        return service.pageQuery(in);
    }

    @Operation(summary = "根据ID列表查询配电房列表", description = "根据ID列表查询配电房列表")
    @PostMapping("/list")
    public ResponseResult<List<ElectricalRoom>> listByIds(@RequestBody List<String> ids) {
        return service.listByIds(ids);
    }

    @Operation(summary = "分页查询配电房列表（带统计信息）", description = "分页查询配电房列表（带统计信息）")
    @GetMapping("/pageWithStats")
    public ResponseResult<IPage<ElectricalRoomWithStatisticDTO>> pageQueryWithStats(
            @Parameter(description = "页码", example = "1")
            @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量", example = "10")
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @Parameter(description = "组织ID", example = "org-01")
            @RequestParam(name = "orgId", required = true) String orgId,
            @Parameter(description = "巡检工单ID", example = "inspection-order-01")
            @RequestParam(name = "inspectionOrderId", required = true) String inspectionOrderId) {
        return service.pageQueryWithStats(pageNum, pageSize, orgId, inspectionOrderId);
    }
}